package com.servers;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.po.Person;

public class HttpServer {
	public static void main(String[] args) throws Exception {
		// String s="<span class=\"123class\">start</span></div>";
		// String r="(\\{\\{(.+?)\\}\\})";
		// Pattern
		// p=Pattern.compile(r,Pattern.CASE_INSENSITIVE);//CASE_INSENSITIVE所有字母可以大写或者小写,正则表达式
		// Matcher m=p.matcher(s);//操作的字符串
		// System.out.println(s.length());
		// System.out.println(m.find());
		// System.out.println(m.group(1));//在java中使用正则表达式返回符合正则表达式的字符串就要用到group()，group中记录了所有符合指定表达式的字符串
		// System.out.println(m.group(2));
		// 常见的使用方式
		// while(m.find()){
		// System.out.println(m.group(1));
		// }

		// System.out.println(m);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ServerSocket server = new ServerSocket(80);

		while (true) {
			Socket sk = server.accept();
			System.out.println("client in...");
			OutputStream out = sk.getOutputStream();

			// 发送HTTP response header
			out.write("HTTP/1.0 200 OK\r\n".getBytes());
			out.write("Content-Type:text/html\r\n".getBytes());
			out.write("\r\n".getBytes());

			InputStream webin = sk.getInputStream();
			Date date = new Date();
			int i = webin.available();// 定义页面输入字节流的大小
			if (i != 0) {
				String message;
				byte[] ws = new byte[webin.available()];// 实例化一个字节数组ws并给字节数组ws分配空间大小
				webin.read(ws);//
				message = new String(ws, "utf8");
				System.out.println(i);
				System.out.println(message);
				if (ServersForDatabase.isopen) {
					if (message.contains("GET /stop")) {
						ServersForDatabase.closeServer();
						System.out.println("Close 8888"+ServersForDatabase.isopen);
						InputStream in = new FileInputStream("start.html");
						byte[] bs = new byte[in.available()];
						in.read(bs);
						out.write(bs);
						in.close();
					} else {
						InputStream in = new FileInputStream("stop.html");
						byte[] bs = new byte[in.available()];
						in.read(bs);
						Map<String, Person> mapPerson = ServersForDatabase.getMapPerson();
						
						StringBuilder tmpString = new StringBuilder();
						tmpString.append("<br>\n Server is open at:"+formatter.format(date));
						tmpString.append("<br>\n Ip List:");
						tmpString.append("<br>\n Username ip LoginTime");
						for(Map.Entry<String, Person> entry : mapPerson.entrySet()){
							tmpString.append("<br>\n"+mapPerson.get(entry.getKey()).getPname()+"");
							tmpString.append(entry.getKey()+"");
							tmpString.append(formatter.format(mapPerson.get(entry.getKey()).getLogin_time()));
							System.out.println("Nothing");
						}
//						System.out.println("HAHAHAA");
						System.out.println(tmpString.toString());
						out.write(bs);
						out.write(tmpString.toString().getBytes());
						tmpString.setLength(0);
						in.close();
					}
				} else {
					System.out.println("Didn't open");
					if (message.contains("GET /start")) {
						date = new Date();
						new Thread(){
							public void run() {
								try {
									ServersForDatabase.openServersForDatabase();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}.start();
						System.out.println("Open 8888");
						InputStream in = new FileInputStream("stop.html");
						byte[] bs = new byte[in.available()];
						in.read(bs);
						out.write(bs);
						in.close();
					} else {
						InputStream in = new FileInputStream("start.html");
						byte[] bs = new byte[in.available()];
						in.read(bs);
						out.write(bs);
						in.close();
					}
				}

				System.out.println(i);
				System.out.println(message);

			}

		}
		// server.close();

	}

}
